package com.blog.frontend.pages.parts;

import org.apache.log4j.Logger;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

import com.blog.frontend.pages.ViewArticlePage;
import com.blog.jdo.ArticleDAO;
import com.blog.model.Article;

public class ArticleEditor extends Panel {
	private static final long serialVersionUID = -513707596483603879L;
	
	private static final Logger log = Logger.getLogger(ArticleEditor.class);
	
	private final Form form;
	
	private static ArticleDAO articleDAO = new ArticleDAO();
	
	public ArticleEditor(final String id, Article a) {
		super(id);
		
		final boolean newMode = a == null;
		
		if (newMode) {
			a = new Article();
		}
		
		form = new Form("form") {
			private static final long serialVersionUID = -2081493432411829545L;
			
			@Override
			protected void onSubmit() {
				log.info("onSubmit()");
				final Article article = (Article) getModelObject();
				log.info("modelObject = " + article);
				article.setCompiled(false);
				
				articleDAO.saveArticle(article);
				
				setResponsePage(new ViewArticlePage(article));
			}
		};
		
		form.add(new SubmitLink("saveLink"));
		
		form.add(new RequiredTextField("title"));
		final TextArea source = new TextArea("source");
		form.add(source);
		
		form.setModel(new CompoundPropertyModel(a));
		
		add(form);
		add(new Label("id", a.getId() == null ? "new article" : a.getId().toString()));
	}
	
	public void setArticle(final Article a) {
		
	}
	
}